package com.amgl.recyclerviewwithheaderfooter;

import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * RecyclerView 添加HeaderView和FooterView
 * Created by amglh on 2016/8/10.
 */
public class HeaderAndFooterUtils {
    /**
     * 设置HeaderView
     * <p/>
     * 只能设置一个，重复设置抛异常
     *
     * @param recyclerView
     * @param headerView
     */
    public static void setHeaderView(RecyclerView recyclerView, View headerView) {
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        if (adapter == null) {
            throw new RuntimeException("adapter can not be null");
        }

        if (adapter instanceof HeaderAndFooterRecyclerViewAdapter) {
            HeaderAndFooterRecyclerViewAdapter hafAdapter = (HeaderAndFooterRecyclerViewAdapter) adapter;
            if (hafAdapter.getHeadersCount() > 0) {
                throw new RuntimeException("already have a header view");
            }
            hafAdapter.addHeaderView(headerView);
        } else {
            throw new RuntimeException("please use HeaderAndFooterRecyclerViewAdapter");
        }
    }

    /**
     * 设置FooterView
     * <p/>
     * 只能设置一个，重复设置抛异常
     *
     * @param recyclerView
     * @param footerView
     */
    public static void setFooterView(RecyclerView recyclerView, View footerView) {
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        if (adapter == null) {
            throw new RuntimeException("adapter can not be null");
        }

        if (adapter instanceof HeaderAndFooterRecyclerViewAdapter) {
            HeaderAndFooterRecyclerViewAdapter hafAdapter = (HeaderAndFooterRecyclerViewAdapter) adapter;
            if (hafAdapter.getFootersCount() > 0) {
                throw new RuntimeException("already have a footer view");
            }
            hafAdapter.addFootView(footerView);
        } else {
            throw new RuntimeException("please use HeaderAndFooterRecyclerViewAdapter");
        }
    }

    public static int getLayoutPosition(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        if (adapter != null && adapter instanceof HeaderAndFooterRecyclerViewAdapter) {
            HeaderAndFooterRecyclerViewAdapter hafAdapter = (HeaderAndFooterRecyclerViewAdapter) adapter;
            int headerCount = hafAdapter.getHeadersCount();
            return viewHolder.getLayoutPosition() - headerCount;
        }
        return viewHolder.getLayoutPosition();
    }

    public static int getAdapterPosition(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        if (adapter != null && adapter instanceof HeaderAndFooterRecyclerViewAdapter) {
            HeaderAndFooterRecyclerViewAdapter hafAdapter = (HeaderAndFooterRecyclerViewAdapter) adapter;
            int headerCount = hafAdapter.getHeadersCount();
            return viewHolder.getAdapterPosition() - headerCount;
        }
        return viewHolder.getAdapterPosition();
    }
}
